$(document).ready(function ($) {
    var _page = $('.page');

    var user_obj = JSON.parse($(_page).find('input[name=user_obj]').val());
    var target_obj = JSON.parse($(_page).find('input[name=target_obj]').val());
    if (!target_obj.user_id || !target_obj.user_id) {
        return bottlecms.toast("信息错误");
    }


    $(_page).on('click', '.js-del_history', function () {
        delete_chat_fun(target_obj.user_id);
    });

    $(_page).on('click', '.js-handle', function () {
        bottlecms.actions([
            [{
                text: '查看资料',
                onClick: function (modal, e) {
                    bottlecms.goto('/single/info/' + target_obj.user_id);
                }
            }, {
                text: '删除聊天记录',
                onClick: function (modal, e) {
                    delete_chat_fun(target_obj.user_id);
                }
            }],
            [{
                text: '取消',
                bg: 'danger'
            }]
        ]);
    });

    var dialogue = null;
    dialogue = bottlecms.dialogue.init({
        elem: $(_page).find(".bcui-dialogue"),
        me: user_obj,
        he: target_obj
    });

});


var _page = $('.page')

var loadobj = function () {
    var page = 0;
    bottlecms.get("/m/ajax/center/chat", {
        page: page
    }, function (data) {
        var lis = [];
        for (var o in data.list) {
            lis.push(chat_template(data.list[o]))
        }
        $('.bcui-infinite-list').html(lis.join(''));
    })
}

function refreshloadobj() {
    setTimeout(function () {
        bottlecms.log("refresh");
        loadobj();
        refreshloadobj();
    }, 30000)
}

$(document).ready(function ($) {
    loadobj();
    refreshloadobj();
})


var target_id = $("#userId").val();

/*聊天列表*/
function chat_template(v) {
    var html = "";
    html += '<div class="item ' + (target_id == v.userId ? 'active' : '') + '" data-uid="' + v.userId + '">';
    html += '<div class="picbox"><img class="pic" src="' + v.photo + '_200x200.jpg"></div>';
    html += '<div class="uinfo"><div class="nickname _text">' + v.nickname + '</div>' +
        // '<div class="conversation">' + v.msgTitle + '</div>' +
        '</div>';
    html += '<div class="etime">' + (v.createTime) + '</div>';
    if (v.unreadNum > 0) html += '<div class="unreadNum">' + v.unreadNum + '</div>';
    html += '<div class="editbox"><div class="_cp del" data-uid="' + v.userId + '">删除</div></div>';
    html += '</div>';
    return html;
}


$(_page).on('click', '.js-chatlist .item', function (event) {
    var id = $(this).attr('data-uid');
    bottlecms.goto('/center/chat/info/' + id);
});